package com.example.job_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.job_backend.mapper.UserMapper;
import com.example.job_backend.model.Query.UserQuery;
import com.example.job_backend.model.User;
import com.example.job_backend.service.UserService;
import com.example.job_backend.util.PasswordUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String username, String password, String role) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username)
                .eq("password", password)
                .eq("role", role);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public User findByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return userMapper.selectOne(wrapper);
    }

    //实现按条件搜索功能的时候用的，增加分页查询后就不用了，改成getUserByPage了
//    @Override
//    public List<User> searchUser(String username, String realName, String classes) {
//        val wrapper = new QueryWrapper<User>();
//        wrapper.like("username",username)
//                .like("real_name",realName)
//                .like("classes",classes);
//        return userMapper.selectList(wrapper);
//    }

    @Override
    public PageInfo<User> getUserByPage(UserQuery query) {
        PageHelper.startPage(query.getPage(), query.getPageSize());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username", query.getUsername())
                .like("real_name", query.getRealName())
                .like("classes", query.getClasses());
        List<User> list = baseMapper.selectList(wrapper);
        PageInfo<User> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public void changePassword(String username, String oldPassword, String newPassword) {
        String passwordHash = PasswordUtils.md5WithSalt(oldPassword, "04943c669d0e4ca2830e402ceaa6242d");
        String passwordNewHash = PasswordUtils.md5WithSalt(newPassword, "04943c669d0e4ca2830e402ceaa6242d");
        User user = this.findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        if (!Objects.equals(passwordHash, user.getPassword())) {
            throw new RuntimeException("旧密码错误");
        }
        user.setPassword(passwordNewHash);
        userMapper.updateById(user);
    }

    @Override
    public List<User> getAllStudents() {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("role","学生");
        List<User> list=userMapper.selectList(wrapper);
        return list;
    }
}
